package cn.kgc;


import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;


@Slf4j
public class CustomerGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        URI uri = exchange.getRequest().getURI();
        log.info("========请求的路径:"+uri.toString());
        if(uri.toString().startsWith("http://localhost:7900/api/user/doLogin")){
            return chain.filter(exchange);
        }

        //从请求头中的获取token
       String token =  exchange.getRequest().getHeaders().getFirst("token");
        if(token!=null&& !"".equals(token)){
            return chain.filter(exchange);
        }
        //响应结束，返回响应结束标记
        //exchange.getResponse().setComplete();
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
